import sys
from instance import instance
from numpy import array
from util import *

"""
"""

class perceptron:

    def __init__(self, w0, dot = dot, alpha = 0.5, threshold = 0):

        self.alpha = alpha
        self.threshold = threshold
        self.Xs = [array(w0)]
        self.count = [1]
        self.dot = dot
    
    def train(self, instances = None, num_epochs = 1):
        
        for i in range(num_epochs):
            
            self.iteration(instances)
            
    def classify(self, ins):
        
        return self.classify_train(ins)
    
    def classify_train(self, ins):
        
        # is this right ilia????????????????????????????????????????????
        #return sign(sum(map(lambda m_ins: self.dot(ins,m_ins), self.Xs)))
        return sign(dot(sum(self.Xs),ins))
    
    def test(self, instances):
        
        miss = 0
        for x in instances:
            y = self.classify(x.array)
            if(y != x.label): miss+=1
        return miss
        
    def iteration(self,instances):
        
        '''
        runs one epoch of learning.
        '''
        
        for x in instances:
            
            y = self.classify_train(x.array)
            if(y != x.label):
                                          
                self.Xs.append(self.alpha*(x.label)*x.array)
                self.count.append(1)

            else:

                self.count[-1] += 1
